#include "orangepch.h"
#include "OpenGLVertexArray.h"

namespace Orange
{

    OpenGLVertexArray::OpenGLVertexArray()
    {
        glCreateVertexArrays(1, &m_RendererId);
    };
    OpenGLVertexArray::~OpenGLVertexArray()
    {
        glDeleteVertexArrays(1, &m_RendererId);
    };
    void OpenGLVertexArray::Bind()
    {
        glBindVertexArray(m_RendererId);
    };
    void OpenGLVertexArray::UnBind()
    {
        glBindVertexArray(0);
    };

    void OpenGLVertexArray::AddVertexBuffer(const Ref<VertexBuffer> &vertexBuffer)
    {
        glBindVertexArray(m_RendererId);

        vertexBuffer->Bind();

        std::vector<LayoutElement> elements = vertexBuffer->GetLayout().GetElement();
        uint32_t index = 0;
        for (LayoutElement &element : elements)
        {
            glVertexAttribPointer(index, element.GetCount(element.m_Type), ShaderTypeToGLenum(element.m_Type), element.m_Normalized, vertexBuffer->GetLayout().GetStride(), (void *)element.m_Offset);
            glEnableVertexAttribArray(index);
            index++;
        }

        m_VertexBuffer.push_back(vertexBuffer);
    };
    void OpenGLVertexArray::SetIndexBuffer(const Ref<IndexBuffer> &indexBuffer)
    {
         glBindVertexArray(m_RendererId);

         indexBuffer->Bind();
         
         m_indexBuffer = indexBuffer;
    };
    std::vector<Ref<VertexBuffer>> &OpenGLVertexArray::GetVertexBuffer()
    {
        return m_VertexBuffer;
    };
    Ref<IndexBuffer> &OpenGLVertexArray::GetIndexBuffer()
    {
        return m_indexBuffer;
    };

}